package com.jinlejia.erp.ui.mine.contract;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityDetailsGoldenKeyBinding;
import com.jinlejia.erp.ui.common.CallUpDialog;
import com.jinlejia.erp.ui.house.HouseDetailActivity;
import com.jinlejia.erp.utils.CallUtils;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;

/**
 * 金钥匙合同详情(activity)
 */

public class DetailsGoldenKeyActivity extends BaseActivity {

    private CallUpDialog dialog;
    private String owner_phone_number, rscode;

    private ActivityDetailsGoldenKeyBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_details_golden_key);
        binding.setEvent(this);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        getData();
        //房源编号
        binding.contactDetailsHouseNumber.setOnClickListener(view -> {
            Intent intent = new Intent(DetailsGoldenKeyActivity.this, HouseDetailActivity.class);
            intent.putExtra("code", rscode);
            intent.putExtra("type", "sale");
            startActivity(intent);
        });
    }

    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back_btn:                  //返回
                finish();
                break;
            case R.id.contact_details_link2:      //业主电话
                dialog = new CallUpDialog(DetailsGoldenKeyActivity.this, owner_phone_number);
                dialog.setConfirmListener(() -> callTask(owner_phone_number));
                dialog.show();
                break;
            case R.id.divide_into:               //金钥匙分成
                intent = new Intent(DetailsGoldenKeyActivity.this, GoldenKeyIntoActivity.class);
                intent.putExtra("pactCode", getIntent().getStringExtra("pactCode"));
                startActivity(intent);
                break;
        }
    }

    /**
     * 获取数据
     */
    private void getData() {
        OkHttpUtils
                .get()
                .url(ApiConfig.DETAIL_GOLDEN_KEY_CONTRACT)
                .addParams("pactCode", getIntent().getStringExtra("pactCode"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(DetailsGoldenKeyActivity.this, response, null)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            binding.contactDetailsNumber.setText(data.getString("pactCode"));
                            binding.contactDetailsHouseNumber.setText(data.getString("rscode"));
                            if (!TextUtils.isEmpty(data.getString("rscode")))
                                rscode = data.get("rscode").toString();
                            binding.detailsHouseName.setText(data.getString("ownerName"));
                            binding.contactDetailsPeople.setText(data.getString("signUserName"));
                            binding.contactDetailsAddress.setText(data.getString("shareStoreName"));
                            binding.contactDetailsPropertyAddress.setText(data.getString("address"));
                            switch (data.getString("pactType")) {
                                case "0":
                                    break;
                                case "1":
                                    break;
                                case "2":
                                    binding.contactDetailsState.setText("金钥匙合同");
                                    break;
                            }
                            if (!TextUtils.isEmpty(data.getString("ownerTel"))) {
                                owner_phone_number = data.get("ownerTel").toString();
                            }
                            if (data.getIntValue("binding.commissionState") == 3) {
                                binding.commissionState.setText("结件结佣");
                            } else {
                                binding.commissionState.setVisibility(View.GONE);
                            }

                        }
                    }
                });
    }

    public void callTask(String phone) {
        CallUtils.CallTel(DetailsGoldenKeyActivity.this, phone);

    }

}
